home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: dmocc2.on.bell.ca!bc2cep!news
- From: g2david@sprynet.com (David Leung)
- Subject: Re: Friend vs. Member function for operator overload
- X-Nntp-Posting-Host: duncjm.on.bell.ca
- Message-ID: <Dpr5Ex.CC9@on.bell.ca>
- Sender: news@on.bell.ca (news admin)
- Reply-To: g2david@sprynet.com (David Leung)
- Organization: Bell Canada, Bell Sygma, SRCI
- X-Newsreader: IBM NewsReader/2 v1.02
- References: <4kj55n$mv2@homenet.hom.net>
- Date: Fri, 12 Apr 1996 13:56:57 GMT
-
- In <4kj55n$mv2@homenet.hom.net>, dengel@hom.net (Daniel P. Engel III) writes:
- >Can anyone tell me the relative advantages and disadvantages of using
- >a friend function vs. a member function to overload an operator, such
- >as addition? For example, suppose you have a Complex class (I'm not
- >interested in building a complex class--I'm just using this for an
- >example.):
- >
- >class Complex {
- >private:
- > int real, imag;
- >.. . .
- >};
- >
- >Now, you would want to be able to add complex numbers, like so:
- >
- >Complex a, b, c;
- >
- >a = (some complex number);
- >b = (some complex number);
- >c = a + b;
- >
- >Now, as far as I can tell, there are two general approaches to this.
- >One is the declare a friend function header in the class definition:
- >
- >friend Complex operator+( Complex& a, Complex& b) ;
- >
- >and then define that function something like:
- >
- >Complex operator+( Complex& const a, Complex& const b) {
- > Complex temp;
- > temp.real = a.real + b.real;
- > temp.imag = a.imag + b.imag;
- > return temp;
- >}
- >
- >The other way is to put the operator in the class definition itself:
- >
- >Complex operator+( Complex& const a) {
- > Complex temp;
- > temp.real = this->real + a.real;
- > temp.imag = this->imag + a.imag;
- > return temp;
- >}
- >
- >Can anyone tell me if one of these the "preferred" way of doing it?
- >And, what are the advantages and disadvantages of each?
- >
-
-
- I find an answer for you...
-
-
- From Book "Black Belt C++: The Master Collection"
- Chapter "Special member functions and programming technique"
- by Bruce Eckel
-
- Excerpt from this paper:
-
- Member versus Friend Operators
-
- When overloading operators, many people get confused about whether to use member functions or friend functions.
- There is a difference- the compiler can do conversion for both operands of a friend function,
- whereas it can only convert the right-hand argument of a member function. The left-hand argument of a member
- function must always be an object of the class type.
-
- This means you can control the use of the operator by choosing a friend or member function. In the following
- example, you can only add apple, you can't add oranges to apples. On the other hand, operator-() is a friend
- function so it is reflectie, and since there is a way to convert from an orange to apple
- (the conversion constructor in apple) you can subtract apples and oranges in any combination.
-
- struct orange {};
-
- struct apple {
- void operator+(orange) {}
- friend void operator-(apple,apple) {}
- apple(orange) {} // make apple from orange
- apple() //must create a default
- };
-
- void main() {
- apple a;
- orange o;
- a + o; // this works
- // o + a; // this doesn't
- a - a works;
- a - 0
- o - a
- o - o all works
- }
-
- the last statement is a little surprising. You can even subtract an orange from an orange, even
- though there's no operator-() defined for orange! automatic type conversion is happening for both
- arguments. This is another situation that points out that automatic type conversion can
- cause tricky things to happen.
-
- Note: Structs are used here where normally the kind of information necessary to create
- an apple from an orange would be private. In the case, orange would have to grant the apple constructor
- friend status. This connects the two classes a little more closely and gives the reader a cue.
-